package org.mybatis.generator.utils;

import java.io.File;

/**
 * @author hedong
 * @date 2021/1/22
 */
public class FileUtils {

    public static void emptyDir(File dir) {
        if (!dir.isDirectory()) return;
        File[] files = dir.listFiles();
        for (File file : files) {
            deleteRecursive(file);
        }
    }

    public static void deleteRecursive(File file) {
        while (file.exists()) {
            deleteLeafFile(file);
        }
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static void deleteLeafFile(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.list().length == 0) {
            file.delete();
            return;
        }
        for (File file1 : file.listFiles()) {
            deleteLeafFile(file1);
        }
    }

    public static void main(String[] args) {
        emptyDir(new File("D:\\code\\mybatis-plus-generator\\out\\main\\resources\\mapper"));
        emptyDir(new File("D:\\code\\mybatis-plus-generator\\out\\main\\java"));
    }
}
